# @Filename:    operate_csv.py
# @Author:      王佳伟
# @Time:        2025-07-21 9:25
# @Describe:    csv读写文件
import pandas as pd
import numpy as np

# read_csv() 从 CSV 文件中读取数据，并创建 DataFrame 对象
# 需要注意文件的路径
# df = pd.read_csv("D:/repo/python-study/pandas-study/person.csv")
# print(df)

# 自定义索引
# df = pd.read_csv("D:/repo/python-study/pandas-study/person.csv", index_col=['ID'])
# print(df)

# 转换salary为float类型
# df = pd.read_csv("D:/repo/python-study/pandas-study/person.csv", dtype={'Salary':np.float64})
# print(df.dtypes)

# 更改文件标头名
# df = pd.read_csv("D:/repo/python-study/pandas-study/person.csv",names=['a','b','c','d','e'])
# print(df)

# 删除原来的标头名
# df = pd.read_csv("D:/repo/python-study/pandas-study/person.csv",names=['a','b','c','d','e'],header=0)
# print(df)

# 跳过指定的行数
# df = pd.read_csv("D:/repo/python-study/pandas-study/person.csv",skiprows=2)
# print(df)

# to_csv 将 DataFrame 转换为 CSV 数据
# 如果想要把 CSV 数据写入文件，只需向函数传递一个文件对象即可。否则，CSV 数据将以字符串格式返回

data = {'Name': ['Smith', 'Parker'], 'ID': [101, 102], 'Language': ['Python', 'JavaScript']}
info = pd.DataFrame(data)
print('DataFrame Values:\n', info)
# 转换为csv数据
# csv_data = info.to_csv()
# print('\CSV String Values:\n', csv_data)

# 指定 CSV 文件输出时的分隔符，并将其保存在 pandas.csv 文件中
csv_data = info.to_csv("D:/repo/python-study/pandas-study/pandas.csv",sep=',')
